<template>
  <div class="login-container" :class="{ 'dark-mode': isDarkMode }">
    <div class="login-background">
      <div class="animated-shapes">
        <div class="shape shape-1"></div>
        <div class="shape shape-2"></div>
        <div class="shape shape-3"></div>
      </div>
      <div class="particles">
        <div class="particle" v-for="i in 20" :key="i" :style="getParticleStyle(i)"></div>
      </div>
    </div>
    
    <div class="login-wrapper">
      <div class="login-form-wrapper">
        <div class="theme-toggle" @click="toggleDarkMode">
          <BulbOutlined v-if="isDarkMode" />
          <BulbFilled v-else />
        </div>
        <div class="login-header">
          <div class="login-logo">
            <div class="logo-circle">
              <FileTextOutlined class="logo-icon" />
            </div>
          </div>
          <div class="login-title">Gulu Blog</div>
          <div class="login-subtitle">内容管理系统</div>
          <p class="login-description">欢迎回来，请登录您的账号</p>
        </div>
        
        <div class="login-form-container">
          <a-form
            :model="loginForm"
            :rules="loginRules"
            ref="loginFormRef"
            @finish="handleLogin"
            class="login-form"
          >
            <a-form-item name="username">
              <div class="input-group">
                <label class="input-label">用户名</label>
                <a-input
                  v-model:value="loginForm.username"
                  placeholder="请输入用户名"
                  size="large"
                  class="login-input enhanced-input"
                  autocomplete="username"
                >
                  <template #prefix>
                    <UserOutlined class="input-prefix-icon" />
                  </template>
                </a-input>
              </div>
            </a-form-item>
            
            <a-form-item name="password">
              <div class="input-group">
                <label class="input-label">密码</label>
                <a-input-password
                  v-model:value="loginForm.password"
                  placeholder="请输入密码"
                  size="large"
                  visibilityToggle
                  class="login-input enhanced-input"
                  autocomplete="current-password"
                >
                  <template #prefix>
                    <LockOutlined class="input-prefix-icon" />
                  </template>
                </a-input-password>
              </div>
            </a-form-item>
            
            <a-form-item class="login-options">
              <div class="options-row">
                <a-button type="link" class="forgot-link" @click="handleForgotPassword">
                  忘记密码？
                </a-button>
              </div>
            </a-form-item>
            
            <a-form-item>
              <a-button
                type="primary"
                html-type="submit"
                :loading="loading"
                block
                size="large"
                class="login-button enhanced-button"
              >
                <LoginOutlined v-if="!loading" class="button-icon" />
                {{ loading ? '登录验证中...' : '安全登录' }}
              </a-button>
            </a-form-item>
            
            <div class="login-tips">
              <InfoCircleOutlined class="tips-icon" />
              <span>首次登录请使用管理员分配的账号密码</span>
            </div>
          </a-form>
        </div>
        
        <div class="login-footer">
          <div class="footer-content">
            <p class="footer-text">
              <SafetyCertificateOutlined class="footer-icon" />
              安全加密 · 保护隐私
            </p>
            <p class="copyright">
              &copy; 2024 Gulu Blog 内容管理系统
            </p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, nextTick } from 'vue'
import { message } from 'ant-design-vue'
import { 
  UserOutlined, 
  LockOutlined, 
  FileTextOutlined, 
  LoginOutlined, 
  InfoCircleOutlined,
  SafetyCertificateOutlined,
  BulbOutlined,
  BulbFilled
} from '@ant-design/icons-vue'
import { useRouter } from 'vue-router'
import authApi from '@/api/auth'

const router = useRouter()
const loginFormRef = ref()
const loading = ref(false)
const isDarkMode = ref(false)
const loginForm = reactive({
  username: '',
  password: '',
  remember: false
})

// 检测系统深色模式
const checkSystemDarkMode = () => {
  return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
}

// 切换深色模式
const toggleDarkMode = () => {
  isDarkMode.value = !isDarkMode.value
  document.documentElement.classList.toggle('dark-mode', isDarkMode.value)
  localStorage.setItem('darkMode', isDarkMode.value ? 'dark' : 'light')
}

// 初始化深色模式
onMounted(() => {
  // 检查本地存储的主题偏好
  const savedMode = localStorage.getItem('darkMode')
  if (savedMode) {
    isDarkMode.value = savedMode === 'dark'
  } else {
    // 如果没有保存的偏好，则使用系统设置
    isDarkMode.value = checkSystemDarkMode()
  }
  
  // 应用深色模式
  document.documentElement.classList.toggle('dark-mode', isDarkMode.value)
  
  // 监听系统主题变化
  window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
    if (localStorage.getItem('darkMode') === null) {
      isDarkMode.value = e.matches
      document.documentElement.classList.toggle('dark-mode', isDarkMode.value)
    }
  })
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度必须在3-20个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度必须在6-20个字符之间', trigger: 'blur' }
  ]
}

// 页面加载时检查是否有保存的用户名
onMounted(() => {
  const savedUsername = localStorage.getItem('savedUsername')
  if (savedUsername) {
    loginForm.username = savedUsername
    loginForm.remember = true
  }
})

const handleLogin = async () => {
  try {
    loading.value = true

    // 表单验证
    await loginFormRef.value.validate()

    // 保存用户名（如果选择记住密码）
    if (loginForm.remember) {
      localStorage.setItem('savedUsername', loginForm.username)
    } else {
      localStorage.removeItem('savedUsername')
    }

    // 调用登录API
    const response = await authApi.login({ username: loginForm.username, password: loginForm.password })

    if (response.code === 200 && response.data) {
      message.success('登录成功！正在跳转...')
      // 保存token
      localStorage.setItem('adminToken', response.data.token || 'mock-token')
      // 使用nextTick确保状态更新后再跳转
      await nextTick()

      // 检查是否有重定向路径
      const redirectPath = sessionStorage.getItem('redirectPath')
      if (redirectPath) {
        sessionStorage.removeItem('redirectPath')
        router.push(redirectPath)
      } else {
        // 默认跳转到仪表盘
        router.push('/dashboard')
      }
    } else {
      message.error(response.message || '登录失败')
    }
  } catch (error) {
    message.error(error.message || '登录失败，请重试')
  } finally {
    loading.value = false
  }
}

const handleForgotPassword = () => {
  message.info('请联系管理员重置密码')
}

// 生成粒子动画样式
const getParticleStyle = (index) => {
  const size = Math.random() * 4 + 2
  const left = Math.random() * 100
  const animationDuration = Math.random() * 20 + 10
  const animationDelay = Math.random() * 10
  
  return {
    width: `${size}px`,
    height: `${size}px`,
    left: `${left}%`,
    animationDuration: `${animationDuration}s`,
    animationDelay: `${animationDelay}s`
  }
}
</script>

<style scoped>
/* 主题变量 - 浅色模式 */
:root {
  --primary-color: #1890ff;
  --primary-hover: #40a9ff;
  --primary-active: #096dd9;
  --primary-light: rgba(24, 144, 255, 0.05);
  --primary-shadow: rgba(24, 144, 255, 0.3);
  
  --bg-color: #ffffff;
  --bg-color-secondary: #fafafa;
  --bg-gradient: linear-gradient(135deg, #0050b3 0%, #1890ff 100%);
  --bg-gradient-hover: linear-gradient(135deg, #096dd9 0%, #40a9ff 100%);
  
  --text-color-primary: #262626;
  --text-color-secondary: #666666;
  --text-color-tertiary: #999999;
  --text-color-quaternary: #bfbfbf;
  
  --border-color: #d9d9d9;
  --border-color-light: #f0f0f0;
  --border-color-focus: #1890ff;
  
  --shadow-color: rgba(0, 0, 0, 0.09);
  --shadow-color-hover: rgba(0, 0, 0, 0.12);
  
  --success-color: #52c41a;
  
  --logo-text-color: #1890ff;
  --welcome-text-color: #666666;
  --button-text-color: #ffffff;
  --shape-color-1: rgba(24, 144, 255, 0.15);
  --shape-color-2: rgba(24, 144, 255, 0.1);
  --shape-color-3: rgba(24, 144, 255, 0.05);
  
  /* SVG图标专用颜色变量 - 白天模式 */
  --icon-theme-toggle: #1890ff;
  --icon-logo: #ffffff;
  --icon-input-prefix: #1890ff;
  --icon-button: #ffffff;
  --icon-tips: #1890ff;
  --icon-footer: #52c41a;
  --icon-secondary: #666666;
}

/* 深色模式变量 */
@media (prefers-color-scheme: dark) {
  :root {
    --primary-color: #177ddc;
    --primary-hover: #1890ff;
    --primary-active: #0050b3;
    --primary-light: rgba(23, 125, 220, 0.1);
    --primary-shadow: rgba(23, 125, 220, 0.3);
    
    --bg-color: #141414;
    --bg-color-secondary: #1f1f1f;
    --bg-gradient: linear-gradient(135deg, #40a9ff 0%, #69c0ff 100%);
    --bg-gradient-hover: linear-gradient(135deg, #69c0ff 0%, #91d5ff 100%);
    
    --text-color-primary: #ffffff;
    --text-color-secondary: #e6e6e6;
    --text-color-tertiary: #b3b3b3;
    --text-color-quaternary: #8c8c8c;
    
    --border-color: #434343;
    --border-color-light: #303030;
    --border-color-focus: #177ddc;
    
    --shadow-color: rgba(0, 0, 0, 0.45);
    --shadow-color-hover: rgba(0, 0, 0, 0.55);
    
    --success-color: #49aa19;
    
    --logo-text-color: #40a9ff;
    --welcome-text-color: #e6e6e6;
    --button-text-color: #000000;
    --shape-color-1: rgba(23, 125, 220, 0.15);
    --shape-color-2: rgba(23, 125, 220, 0.1);
    --shape-color-3: rgba(23, 125, 220, 0.05);
    
    /* SVG图标专用颜色变量 - 深色模式 */
    --icon-theme-toggle: #40a9ff;
    --icon-logo: #ffffff;
    --icon-input-prefix: #40a9ff;
    --icon-button: #000000;
    --icon-tips: #40a9ff;
    --icon-footer: #49aa19;
    --icon-secondary: #e6e6e6;
  }
}

.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  position: relative;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
  color: var(--text-color-primary);
}

/* 动态背景 */
.login-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg-gradient);
  z-index: 1;
  overflow: hidden;
  transition: background-color 0.3s ease;
}

.animated-shapes .shape {
  position: absolute;
  border-radius: 50%;
  background-color: var(--shape-color-2);
  transition: background-color 0.3s ease;
}

.animated-shapes .shape-1 {
  width: 300px;
  height: 300px;
  top: -150px;
  left: -150px;
  background-color: var(--shape-color-1);
}

.animated-shapes .shape-2 {
  width: 200px;
  height: 200px;
  bottom: -100px;
  right: -100px;
  background-color: var(--shape-color-2);
}

.animated-shapes .shape-3 {
  width: 150px;
  height: 150px;
  bottom: 50px;
  left: 10%;
  background-color: var(--shape-color-3);
}

/* 登录卡片 */
.login-wrapper {
  position: relative;
  z-index: 2;
  width: 100%;
  max-width: 420px;
  padding: 0 20px;
  animation: fadeIn 0.5s ease-out;
}

.theme-toggle {
  position: absolute;
  top: 20px;
  right: 20px;
  font-size: 24px;
  cursor: pointer;
  color: var(--icon-theme-toggle);
  z-index: 10;
  transition: all 0.3s ease;
  border-radius: 50%;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-form-wrapper {
  background: var(--bg-color);
  border-radius: 8px;
  box-shadow: 0 2px 8px var(--shadow-color);
  overflow: hidden;
  border: 1px solid var(--border-color-light);
  transition: all 0.3s ease;
  position: relative;
}

.login-form-wrapper:hover {
  box-shadow: 0 4px 16px var(--shadow-color-hover);
}

.login-header {
  text-align: center;
  padding: 24px;
  background: var(--bg-color-secondary);
  border-bottom: 1px solid var(--border-color-light);
}

.logo-circle {
  width: 64px;
  height: 64px;
  margin: 0 auto 16px;
  background: var(--bg-gradient);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px var(--primary-shadow);
}

.logo-icon {
  font-size: 32px;
  color: var(--icon-logo);
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
}

.login-title {
  font-size: 24px;
  font-weight: 500;
  color: var(--text-color-primary);
  margin-bottom: 8px;
}

.login-subtitle {
  font-size: 14px;
  color: var(--primary-color);
  font-weight: 500;
  margin-bottom: 4px;
}

.login-description {
  font-size: 14px;
  color: var(--text-color-secondary);
  margin: 0;
}

/* 表单区域 */
.login-form-container {
  padding: 24px;
}

.login-form {
  width: 100%;
}

.input-group {
  margin-bottom: 8px;
}

.input-label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-color-primary);
  margin-bottom: 8px;
}

.enhanced-input {
  border-radius: 6px;
  border: 1px solid var(--border-color);
  font-size: 14px;
  height: 40px;
  transition: all 0.3s ease;
  background: var(--bg-color);
  color: var(--text-color-primary);
}

.enhanced-input:hover {
  border-color: var(--primary-color);
}

.enhanced-input:focus {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px var(--primary-shadow);
  background: var(--bg-color);
}

.input-prefix-icon {
  color: var(--icon-input-prefix);
  font-size: 16px;
  transition: color 0.3s ease;
}

.options-row {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-bottom: 24px;
}

.remember-checkbox {
  font-size: 14px;
}

.checkbox-label {
  color: var(--text-color-secondary);
  font-size: 14px;
}

.forgot-link {
  color: var(--primary-color);
  font-size: 14px;
  font-weight: 500;
  padding: 0;
  height: auto;
}

.forgot-link:hover {
  color: var(--primary-hover);
}

.enhanced-button {
  height: 40px;
  font-size: 14px;
  font-weight: 600;
  border-radius: 6px;
  background: var(--bg-gradient);
  border: none;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--button-text-color);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.enhanced-button:hover:not(:disabled) {
  background: var(--bg-gradient-hover);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px var(--primary-shadow);
}

.enhanced-button:active:not(:disabled) {
  transform: translateY(0);
}

.button-icon {
  font-size: 14px;
  color: var(--icon-button);
  transition: color 0.3s ease;
}

/* 覆盖Ant Design Vue加载状态的样式 */
:deep(.ant-btn-loading) {
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  gap: 8px !important;
}

:deep(.ant-btn-loading .anticon-loading) {
  margin-right: 8px !important;
}

.login-tips {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-top: 20px;
  padding: 12px;
  background: var(--primary-light);
  border-radius: 6px;
  font-size: 13px;
  color: var(--primary-color);
}

.tips-icon {
  font-size: 14px;
  color: var(--icon-tips);
  transition: color 0.3s ease;
}

.login-footer {
  text-align: center;
  padding: 0 24px 24px;
}

.footer-content {
  border-top: 1px solid var(--border-color-light);
  padding-top: 16px;
}

.footer-text {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-color-tertiary);
  margin-bottom: 8px;
}

.footer-icon {
  font-size: 14px;
  color: var(--icon-footer);
  transition: color 0.3s ease;
}

.copyright {
  font-size: 12px;
  color: var(--text-color-quaternary);
  margin: 0;
}

/* 动画 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 浅色模式增强样式 - 提升SVG和按钮可见性 */
.login-container:not(.dark-mode) .theme-toggle {
  background: rgba(255, 255, 255, 0.95);
  box-shadow: 0 2px 8px rgba(24, 144, 255, 0.15);
  border: 1px solid rgba(24, 144, 255, 0.2);
}

.login-container:not(.dark-mode) .theme-toggle:hover {
  background: rgba(255, 255, 255, 1);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.25);
  transform: translateY(-1px);
}

.login-container:not(.dark-mode) .logo-icon {
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.login-container:not(.dark-mode) .input-prefix-icon {
  font-weight: 600;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.1));
}

.login-container:not(.dark-mode) .button-icon {
  font-weight: 600;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.login-container:not(.dark-mode) .tips-icon {
  font-weight: 600;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.1));
}

.login-container:not(.dark-mode) .footer-icon {
  font-weight: 600;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.1));
}

.login-container:not(.dark-mode) .enhanced-button {
  box-shadow: 0 2px 8px rgba(24, 144, 255, 0.3);
  border: 1px solid rgba(0, 80, 179, 0.2);
  font-weight: 700;
}

.login-container:not(.dark-mode) .enhanced-button:hover:not(:disabled) {
  box-shadow: 0 6px 16px rgba(24, 144, 255, 0.4);
  border: 1px solid rgba(0, 80, 179, 0.3);
}

.login-container:not(.dark-mode) .login-tips {
  border: 1px solid rgba(24, 144, 255, 0.3);
  box-shadow: 0 2px 4px rgba(24, 144, 255, 0.1);
}

/* .login-container:not(.dark-mode) .login-form-wrapper {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  border: 1px solid rgba(0, 0, 0, 0.06);
}

.login-container:not(.dark-mode) .login-form-wrapper:hover {
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
} */

.login-container:not(.dark-mode) .enhanced-input {
  border: 1px solid #d9d9d9;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  background: #ffffff;
}

.login-container:not(.dark-mode) .enhanced-input:hover {
  border-color: #40a9ff;
  box-shadow: 0 2px 6px rgba(24, 144, 255, 0.15);
}

.login-container:not(.dark-mode) .enhanced-input:focus {
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2), 0 2px 6px rgba(24, 144, 255, 0.15);
}

.login-container:not(.dark-mode) .forgot-link {
  color: #0050b3;
  font-weight: 600;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.login-container:not(.dark-mode) .forgot-link:hover {
  color: #096dd9;
  text-decoration: underline;
}

.login-container:not(.dark-mode) .remember-checkbox {
  font-weight: 500;
}

.login-container:not(.dark-mode) .input-label {
  color: #262626;
  font-weight: 600;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.login-container:not(.dark-mode) .login-title {
  color: #262626;
  font-weight: 600;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.login-container:not(.dark-mode) .login-subtitle {
  color: #0050b3;
  font-weight: 600;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.login-container:not(.dark-mode) .login-description {
  color: #595959;
  font-weight: 500;
}

/* 深色模式特定样式 */
.dark-mode .input-label {
  color: #ffffff !important;
  font-weight: 600;
}

.dark-mode .checkbox-label {
  color: #ffffff !important;
  font-weight: 500;
}

.dark-mode .login-tips {
  background: rgba(64, 169, 255, 0.15) !important;
  color: #ffffff !important;
  border: 1px solid rgba(64, 169, 255, 0.3);
}

.dark-mode .login-tips span {
  color: #ffffff !important;
}

.dark-mode .footer-text {
  color: #ffffff !important;
}

.dark-mode .copyright {
  color: #e6e6e6 !important;
}

.dark-mode .login-title {
  color: #ffffff !important;
}

.dark-mode .login-description {
  color: #e6e6e6 !important;
}

.dark-mode .login-subtitle {
  color: #40a9ff !important;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-wrapper {
    padding: 0 16px;
  }
  
  .login-form-wrapper {
    border-radius: 8px;
  }
  
  .login-header {
    padding: 20px;
  }
  
  .login-form-container {
    padding: 20px;
  }
  
  .login-title {
    font-size: 20px;
  }
  
  .options-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  
  .login-footer {
    padding: 0 20px 20px;
  }
}
</style>